#!/usr/local/bin/python3

import time
import json
import os
import sys
import shutil

def createFolders(parent):
    if not os.path.exists(parent):
        os.mkdir(parent)
        os.mkdir(parent + "/" + parent)
        os.mkdir(parent + "/" + '/thumbnails')
        os.mkdir(parent + "/" + parent + '/script')

def createScript(filename):
    fo = open(filename,"w")
    contents=[
        "--[[",
        "   create at " + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
        "   author:刘易周",
        "   @brief:",
        "--]]",
        "",

        "g_PreRes = nil",

        "--call once when scene loaded",
        "function initialize(scene)",
        "  g_PreRes = scene:getResolution()",
        "  local kuruEngineInstance = KuruEngine.getInstance()",
        "  kuruFaceDetector = KuruFaceDetectorExtension.cast(kuruEngineInstance:getExtension('KuruFaceDetector'))",
        "  kuruTouch = KuruTouchExtension.cast(kuruEngineInstance:getExtension('KuruTouch'))",
        "  kuruTouch:getTouchDownEvent():addEventHandler(onTouchDown)",
        "end",
        "",

        "--call every frame,elapsed time is the time elapsed this frame",
        "function frameReady(scene,elapsedTime)",
        "end",
        "",

        "--call when reset scene,e.g switch to rear camera",
        "function reset(scene)",
        "end",
        "",

        "--call when scene close",
        "function finalize(scene)",
        "  kuruTouch:getTouchDownEvent():removeEventHandler(onTouchDown)",
        "end",
        "",

        "--call when touch screen",
        "function onTouchDown(event)",
        "  print('touchdown')",
        "end",
        "",

        "function addNodeAndRelease(scene, node)",
        "  scene:addNode(node)",
        "  node:release()",
        "  return node",
        "end",
        "",

        "function addChildAndRelease(parent, child)",
        "  parent:addChild(child)",
        "  child:release()",
        "  return child",
        "end"
    ]

    for line in contents:
        fo.write(line + "\n")
    fo.close()
    return

def createjsonFile(filename,luafile):
    fo=open(filename,"w")
    data={"script":luafile,"nodes":[],"extensions:":["KuruFaceDetector"]}
    content=json.dumps(data)
    fo.write(content)
    fo.close()

def main():
    helpmsg="create b612 effects and copy effects file to android phone\n" \
    "   \n"\
    "   usage script [update] effectsname\n" \
    "   \n" \
    "   -create    create effects structure and files,then push to android phone\n" \
    "   -update    update effects files on android phone\n"   \
    "   -delete    delete effects files"    \
    "\n"
    androidFileManager = "./androidfilemanager.sh "
    if(len(sys.argv) > 2):
        effects=sys.argv[2]
        if(effects.endswith("/")):
            effects = effects[0:len(effects) - 1]
        if(effects.find("/") != -1):
            print("error input name..please input a name like 'heart'")
            return
        if(sys.argv[1] == "-u"):
            os.system(androidFileManager + "-push " + effects + "/" + effects)
        elif(sys.argv[1] == "-c"):
            if(os.path.exists(effects)):
                print("exists '" + effects+ "'.......can't create....")
            else:
                createFolders(effects)
                luafile="index.lua"
                createScript(effects + "/" + effects + "/script/" + luafile)
                createjsonFile(effects + "/" + effects + "/script/metadata.json",luafile)

                os.system(androidFileManager + "-push " + effects + "/" + effects)
        elif(sys.argv[1] == "-delete"):
            shutil.rmtree(effects)
            os.system(androidFileManager + "-delete " + effects)
        else:
            print(helpmsg)
    else:
        print(helpmsg)

if __name__ == "__main__":
    main()
    